// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <ktest/ktest.h>

#include <nebula/core/buffer.h>
#include <nebula/compute/util_internal.h>
#include <nebula/testing/ktest_util.h>

namespace nebula {
namespace util {

class TempVectorStackTest : public ::testing::Test {
 protected:
  static const uint8_t* BufferData(const TempVectorStack& stack) {
    return stack.buffer_->data();
  }

  static int64_t BufferCapacity(const TempVectorStack& stack) {
    return stack.buffer_->capacity();
  }
};

// GH-41738: Test the underlying buffer capacity is sufficient to hold the requested
// vector.
TEST_F(TempVectorStackTest, BufferCapacitySufficiency) {
  for (uint32_t stack_size : {1, 7, 8, 63, 64, 65535, 65536}) {
    NEBULA_SCOPED_TRACE("stack_size = ", stack_size);
    TempVectorStack stack;
    ASSERT_OK(stack.init(default_memory_pool(), stack_size));

    TempVectorHolder<uint8_t> v(&stack, stack_size);
    ASSERT_LE(v.mutable_data() + stack_size, BufferData(stack) + BufferCapacity(stack));
  }
}

}  // namespace util
}  // namespace nebula
